package com.moxiao.yfjh.module.activity.dal.mysql.activityVerificate;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.mybatis.core.mapper.BaseMapperX;
import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.module.activity.controller.admin.activityVerificate.vo.ActivityVerificateExportReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityVerificate.vo.ActivityVerificatePageReqVO;
import com.moxiao.yfjh.module.activity.controller.app.vo.AppVerificationMeltVo;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityVerificate.ActivityVerificateDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityenrollverificate.ActivityEnrollVerificateDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * 相亲分销信息 Mapper
 *
 * @author mx
 */
@Mapper
public interface ActivityVerificateMapper extends BaseMapperX<ActivityVerificateDO> {

    default PageResult<ActivityVerificateDO> selectPage(ActivityVerificatePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ActivityVerificateDO>()
                .eqIfPresent(ActivityVerificateDO::getNo, reqVO.getNo())
                .eqIfPresent(ActivityVerificateDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ActivityVerificateDO::getActiveId, reqVO.getActiveId())
                .eqIfPresent(ActivityVerificateDO::getPackageId, reqVO.getPackageId())
                .likeIfPresent(ActivityVerificateDO::getActiveName, reqVO.getActiveName())
                .eqIfPresent(ActivityVerificateDO::getVerificateMerchant, reqVO.getVerificateMerchant())
                .eqIfPresent(ActivityVerificateDO::getVerificateUser, reqVO.getVerificateUser())
                .eqIfPresent(ActivityVerificateDO::getVerificateProduct, reqVO.getVerificateProduct())
                .eqIfPresent(ActivityVerificateDO::getVerificatePhr, reqVO.getVerificatePhr())
                .eqIfPresent(ActivityVerificateDO::getVerificateMoney, reqVO.getVerificateMoney())
                .betweenIfPresent(ActivityVerificateDO::getVerificateTime, reqVO.getVerificateTime())
                .eqIfPresent(ActivityVerificateDO::getPackageDetail, reqVO.getPackageDetail())
                .eqIfPresent(ActivityVerificateDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ActivityVerificateDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ActivityVerificateDO::getId));
    }

    default List<ActivityVerificateDO> selectList(ActivityVerificateExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ActivityVerificateDO>()
                .eqIfPresent(ActivityVerificateDO::getNo, reqVO.getNo())
                .eqIfPresent(ActivityVerificateDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ActivityVerificateDO::getActiveId, reqVO.getActiveId())
                .eqIfPresent(ActivityVerificateDO::getPackageId, reqVO.getPackageId())
                .likeIfPresent(ActivityVerificateDO::getActiveName, reqVO.getActiveName())
                .eqIfPresent(ActivityVerificateDO::getVerificateMerchant, reqVO.getVerificateMerchant())
                .eqIfPresent(ActivityVerificateDO::getVerificateUser, reqVO.getVerificateUser())
                .eqIfPresent(ActivityVerificateDO::getVerificateProduct, reqVO.getVerificateProduct())
                .eqIfPresent(ActivityVerificateDO::getVerificatePhr, reqVO.getVerificatePhr())
                .eqIfPresent(ActivityVerificateDO::getVerificateMoney, reqVO.getVerificateMoney())
                .betweenIfPresent(ActivityVerificateDO::getVerificateTime, reqVO.getVerificateTime())
                .eqIfPresent(ActivityVerificateDO::getPackageDetail, reqVO.getPackageDetail())
                .eqIfPresent(ActivityVerificateDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ActivityVerificateDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ActivityVerificateDO::getId));
    }

    /**
     * 获得可以退费的总金额
     *
     * @param userId
     * @param activityId
     * @param status
     */
    @Select("select IFNULL(SUM(price),0.00) from blind_activity_enroll_verificate " +
            " where user_id = #{userId} and activity_id = #{activityId} and status = #{status}")
    BigDecimal getRefundPrice(@Param("userId") Long userId, @Param("activityId") Long activityId, @Param("status") Byte status);

    /**
     * 获得我的核销码列表
     *
     * @param activityId
     * @param userId
     */
    @Select(" select t.id,t.enroll_id,t.user_id,t.activity_id,t.activity_products_id as products_id,t.verification_time," +
            " t.price,t.verification_code,t.`status`,t.type,t1.merchant_id " +
            " FROM blind_activity_enroll_verificate t" +
            " LEFT JOIN  blind_activity_package t1 on t.activity_id = t1.active_id and t.activity_products_id = t1.product_id" +
            " LEFT join blind_activity_enroll t4 on t.enroll_id = t4.id " +
            " where t4.status = 0 and t.status = 0 and t.user_id = #{userId} and t.activity_id = #{activityId} " +
            " order by t1.merchant_id desc ")
    List<AppVerificationMeltVo> getVerList(@Param("activityId") Long activityId, @Param("userId") Long userId);


    /**
     * 创建者-获得我的核销码列表
     *
     * @param activityId
     * @param userId
     */
    @Select(" select t.id,t.enroll_id,t.user_id,t.activity_id,t.activity_products_id as products_id,t.verification_time," +
            " t.price,t.verification_code,t.`status`,t.type,t1.merchant_id " +
            " FROM blind_activity_enroll_verificate t" +
            " LEFT JOIN  blind_activity_package t1 on t.activity_id = t1.active_id and t.activity_products_id = t1.product_id" +
            " LEFT JOIN blind_activity_info t4 on t.activity_id = t4.id" +
            " LEFT JOIN blind_activity_enroll t5 on t.enroll_id = t5.id" +
            " where t5.status = 0 and t.status = 0 and t.activity_id = #{activityId} and t4.head_user_id = #{userId} and t4.cost_share =  2" +
            " order by t1.merchant_id desc")
    List<AppVerificationMeltVo> getCreatorVerList(@Param("activityId") Long activityId, @Param("userId") Long userId);


    @Select(" select t.user_id,t.activity_products_id as products_id,t.verification_time," +
            " t.verification_code,t.`status`,t.type,t1.merchant_id " +
            " FROM blind_activity_enroll_verificate t" +
            " LEFT JOIN blind_activity_package t1 on t.activity_id = t1.active_id and t.activity_products_id = t1.product_id" +
            " where t.user_id = #{userId} and t.activity_id = #{activityId} and t.enroll_id = #{enrollId}")
    List<AppVerificationMeltVo> getCodeList(@Param("activityId") Long activityId, @Param("userId") Long userId, @Param("enrollId") Long enrollId);

    /**
     * app 接口:通过核销码获得核销详情
     *
     * @param code
     * @return
     */
    @Select("select * from blind_activity_enroll_verificate where verification_code = #{code} LIMIT 1")
    ActivityEnrollVerificateDO getCode(@Param("code") String code);
}
